home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / sketch / source / adts / documenthelpers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-04  |  8.1 KB  |  280 lines

  1. /****************************************************************************
  2.  * 
  3.  * DocumentHelpers.c
  4.  * 
  5.  ****************************************************************************/
  6.  
  7. #include "DocumentHelpers.h"
  8.  
  9. #include "Structs.h"
  10. #include "DocumentADT.h"
  11.  
  12. #include "ElementADT.h"
  13.  
  14. #include "WindowUtils.h"
  15.  
  16. #include "Assertion.h"
  17.  
  18. /*****************************************************************************
  19.  * 
  20.  * GetDocumentByWindow                                                  PUBLIC
  21.  * 
  22.  * Given a specified window returns the document that it represents
  23.  *
  24.  *****************************************************************************/
  25. DocumentReference GetDocumentByWindow(DocumentList list, WindowPtr window)
  26. {
  27.     #pragma unused        (list)
  28.     
  29.     DocumentReference document = nil;
  30.     long                    refcon;
  31.     long                    pointer;
  32.     OSType                tag;
  33.     
  34.     if (window != nil)
  35.     {
  36.         refcon =  GetWRefCon((WindowPtr) window);
  37.  
  38.         if (refcon != 0)
  39.         {
  40.             Assert((refcon & 0x0001) == 0, "DOCUMENTS: window refcon is an odd address");
  41.         
  42.             if ((refcon & 0x0001) == 0)                                    // ensure that the refcon is an even address
  43.             {
  44.                 pointer = *((long *)refcon);                            // get what refcon points at
  45.  
  46.                 Assert((pointer & 0x0001) == 0, "DOCUMENTS: window's dereferenced refcon is an odd address");
  47.                 
  48.                 if ((pointer & 0x0001) == 0)                                // ensure that the pointer is an even address
  49.                 {
  50.                     tag = *((OSType *) pointer);                        // extract the tag at the pointer
  51.                     Assert(tag == kDocumentRecordID, "DOCUMENTS: window's dereferenced refcon is not our document tag!");
  52.  
  53.                     if (tag == kDocumentRecordID)                        // is it our document tag?
  54.                         document = (DocumentReference) refcon;        // we can return a valid document!
  55.                 }                
  56.             }
  57.         }
  58.     }
  59.     
  60.     return document;
  61. }
  62.  
  63.  
  64.  
  65.  
  66. /*****************************************************************************
  67.  * 
  68.  * GetDocumentByName                                                    PUBLIC
  69.  *
  70.  *    Returns the document with the given name if found.  Otherwise, nil is returned.
  71.  *
  72.  * • NOTE: we look through the list from frontmost to backmost, which is not really
  73.  * required in a name lookup, but we do it anyway in case there are some non-ad
  74.  * documents in the list, or incase there are two ad documents with the same name.
  75.  *
  76.  *****************************************************************************/
  77. DocumentReference GetDocumentByName(ConstStr63Param nameToFind)
  78. {
  79.     DocumentReference    document     = GetFrontDocument(GetDocumentList());
  80.     Str63                 documentName;
  81.             
  82.     while (document != nil) {
  83.         GetDocumentName(document, documentName);
  84.         if (EqualString(documentName, nameToFind, false, false))
  85.             break;
  86.         document = GetNextFrontDocument(GetDocumentList(), document);
  87.     }
  88.     
  89.     return document;
  90. }
  91.  
  92.  
  93. /*****************************************************************************
  94.  * 
  95.  * GetDocumentByIndex                                                   PUBLIC
  96.  *
  97.  *    Returns the document with the given index if found.  Otherwise, nil is returned.
  98.  * Documents are indexed according to the Macintosh window list, so that document 1
  99.  * is frontmost, document 2 is immediately behind it, etc.
  100.  *
  101.  *****************************************************************************/
  102. DocumentReference GetDocumentByIndex(long index)
  103. {
  104.     DocumentReference    document = GetFrontDocument(GetDocumentList());
  105.     long                    i            = 1L;
  106.             
  107.     while (document != nil && (i < index))
  108.     {
  109.         document = GetNextFrontDocument(GetDocumentList(), document);
  110.         i++;
  111.     }
  112.     
  113.     if (i != index)
  114.         document = nil;
  115.  
  116.     return document;
  117. }
  118.  
  119.  
  120. /*****************************************************************************
  121.  * 
  122.  * GetDocumentByDocumentNumber                                          PUBLIC
  123.  * 
  124.  * Returns the document with the given creation number, if found. Otherwise,
  125.  * nil is returned.
  126.  *
  127.  *****************************************************************************/
  128. OSErr GetDocumentByDocumentNumber(long internalNumber, DocumentReference *document)
  129. {
  130.     OSErr                    error        = noErr;
  131.     
  132.     DocumentReference    tempDocument = GetFirstDocument(GetDocumentList());
  133.     Boolean                foundIt         = false;
  134.     
  135.     while (tempDocument != nil)
  136.     {
  137.         if (internalNumber == GetDocumentNumber(tempDocument))
  138.         {
  139.             foundIt = true;
  140.               break;
  141.         }
  142.         tempDocument = GetNextDocument(tempDocument);
  143.     }
  144.     
  145.     if (foundIt)
  146.         *document = tempDocument;
  147.     else
  148.         *document = nil;
  149.     
  150.     return error;
  151. }
  152.  
  153.  
  154. /*****************************************************************************
  155.  * 
  156.  * GetFrontDocument                                                     PUBLIC
  157.  * 
  158.  * Returns the frontmost document. Note that this does not necessarily mean the
  159.  * frontmost non-floating window since all windows may not have a document
  160.  * associated with it! Note that this function ignores hidden windows
  161.  *
  162.  *****************************************************************************/
  163. DocumentReference GetFrontDocument(DocumentList list)
  164. {
  165.     DocumentReference        document = nil;
  166.     WindowPtr                window;
  167.     
  168.     for (window = FrontWindow(); window != nil; window = GetNextWindow(window))
  169.     {
  170.         document = GetDocumentByWindow(list, window);                            // does this window have a document associated with it?
  171.         if (document != nil)                                                                // if so, then we are done! we have found the frontmost document!
  172.             break;
  173.     }
  174.     
  175.     return document;
  176. }
  177.  
  178.  
  179.  
  180. /*****************************************************************************
  181.  * 
  182.  * GetNextFrontDocument                                                 PUBLIC
  183.  * 
  184.  * Returns the next document following a specified document with respect to
  185.  * window order (NOT with respect to document list order!). Note that this
  186.  * does not necessarily mean the next window’s document since windows may not
  187.  * necessarily have documents associated with them!
  188.  *
  189.  *****************************************************************************/
  190. DocumentReference GetNextFrontDocument(DocumentList list, DocumentReference anchorDocument)
  191. {
  192.     DocumentReference            document            = nil;
  193.     WindowPtr                window;
  194.     WindowPtr                anchorWindow    = GetDocumentWindow(anchorDocument);
  195.     
  196.     for (window = GetNextWindow(anchorWindow); window != nil; window = GetNextWindow(window))
  197.     {                                                                                                // walk the window list after the anchor document looking for more documents
  198.         document = GetDocumentByWindow(list, window);                                // does this window have a document associated with it?
  199.         if (document != nil)                                                                    // if so, then we are done! we have found the frontmost document!
  200.             break;
  201.     }
  202.     
  203.     return document;
  204. }
  205.  
  206. // --------------------------------------------------------------------------
  207.  
  208. DocumentReference GetNextDocument(DocumentReference document)
  209. {
  210.     return GetDocumentNextDocument(document);
  211. }
  212.  
  213. // --------------------------------------------------------------------------
  214.  
  215. DocumentReference GetPreviousDocument(DocumentReference document)
  216. {
  217.     return GetDocumentPreviousDocument(document);
  218. }
  219.  
  220.  
  221. // --------------------------------------------------------------------------
  222.  
  223. Boolean DocumentIsModified(DocumentReference document)
  224. {
  225.     return (GetDocumentNumberOfChanges(document) != 0);
  226. }
  227.  
  228. // --------------------------------------------------------------------------
  229.  
  230. void MarkDocumentAsChanged(DocumentReference document)
  231. {
  232.     SetDocumentNumberOfChanges(document, 1 + GetDocumentNumberOfChanges(document));
  233. }
  234.  
  235.  
  236. // --------------------------------------------------------------------------
  237.  
  238. void InvalDocument(DocumentReference document)
  239. {
  240.     GrafPtr        curPort;
  241.     WindowPtr    window;
  242.     Rect            windowRect;
  243.     
  244.     if (document != nil)
  245.     {
  246.         window = GetDocumentWindow(document);
  247.         if (window != nil)
  248.         {
  249.             GetPort(&curPort);
  250.             SetPort(window);                                                            
  251.             GetWindowPortRect(window, &windowRect);    
  252.             InvalRect(&windowRect);                        
  253.             SetPort(curPort);                                                                
  254.         }
  255.     }
  256. }
  257.  
  258.  
  259. // --------------------------------------------------------------------------
  260.  
  261. void HideDocument(DocumentReference document)
  262. {
  263.     WindowPtr window = GetDocumentWindow(document);
  264.  
  265.     if (window != nil)
  266.         HideWindow(window);
  267. }
  268.  
  269. // --------------------------------------------------------------------------
  270.  
  271. void ShowDocument(DocumentReference document)
  272. {
  273.     WindowPtr window = GetDocumentWindow(document);
  274.     
  275.     if (window != nil)
  276.         ShowWindow(window);
  277. }
  278.  
  279. // --------------------------------------------------------------------------
  280.